<?php
/**
 * Mechanism
 * 
 * LICENSE
 * 
 * This file is private intellectual property of the author. 
 * All rights reserved. Copying and illegal using is punishable by law.
 * 
 * @copyright Copyright (c) 2011 BreatheInMyVoid ( BreatheInMyVoid@gmail.com )
 */

namespace Mechanism\Router {
	use Mechanism\Router\Route;
	/**
	 * Standart router 
	 * 
	 * TODO: Abstract router
	 *
	 * @uses Mechanism\Router\Route
 	 * @package Router
 	 * @author BreatheInMyVoid ( BreatheInMyVoid@gmail.com )
	 */
class Standart implements IRouter {
		/**
		 * 
		 * @var array
		 */
		protected $static_routes = array( );
		
		/**
		 * Dynamic route
		 * 
		 * You can declare many staic routes but only
		 * one dynamic rute can be declarated. This is
		 * our specific policy.
		 * 
		 * @var Mechanism\Router\Route\Dynamic
		 */
		protected $dynamic_route = null;
		
		/**
		 * 
		 */
		protected $module = null;
				
		/**
		 * 
		 */
		protected $controller = null;
		
		/**
		 * 
		 */
		protected $action = null;
		
		/**
		 *
		 */
		protected $vars = null;

		/**
		 * 
		 */
		protected $request = null;
		
		/**
		 * 
		 */
		public function __construct( array $routes = null ) {
			foreach ( $routes as $route ) {
				if ( $route instanceof Route\Stationary ) {
					$this->add_static_route( $route );
				} else if ( $route instanceof Route\Dynamic ) {
					$this->set_dynamic_route( $route );
				} else {
					// BadArgumentException
				}
			}
		}
		
		/**
		 * 
		 */
		public function add_static_route( Route\Stationary $route ) {
			array_push( $this->static_routes, $route );
		}
		
		/**
		 * 
		 * TODO: Only one dynamic rule for one module
		 * 
		 */
		public function set_dynamic_route( Route\Dynamic $route ) {
			$this->dynamic_route = $route;
		}
		
		/**
		 *
		 * TODO: Route simple uri or Request
		 *
		 */
		public function route( $uri ) {
			$is_routed = false;
			$static_routes_len = count( $this->static_routes );
			
			/* At first matching static routes */
			/*
			for ( $i = 0; $i < $static_routes_len; $i++ ) {
				if ( $this->static_routes[ $i ]->match( $this->request ) ) {
					$this->module = $this->static_routes[ $i ]->get_module( );
					$this->controller = $this->static_routes[ $i ]->get_controller( );
					$this->action = $this->static_routes[ $i ]->get_action( );
					$is_routed = true;
				}
			}
			*/

			/* At first matching static routes */
			foreach ( $this->static_routes as $route ) {
				if ( $route->match( $uri ) ) {
					$this->module = $route->get_module( );
					$this->controller = $route->get_controller( );
					$this->action = $route->get_action( );
					$this->vars = $route->get_vars( );
					$is_routed = true;
				}
			}
			if ( !$is_routed ) {
				if ( $this->dynamic_route->match( $uri ) ) {
					$this->module = $this->dynamic_route->get_module( );
					$this->controller = $this->dynamic_route->get_controller( );
					$this->action = $this->dynamic_route->get_action( );
					$this->vars = $this->get_vars( );
					$is_routed = true;
				}				
			}
						
			// if !routed Exception
			return $is_routed;
		}

		/**
		 * 
		 */
		public function get_module( ) {
			return $this->module;
		}
		
		/**
		 * 
		 */
		public function get_controller( ) {
			return $this->controller;	
		}
		
		/**
		 * 
		 */
		public function get_action( ) {
			return $this->action;
		}

		/**
		 *
		 */
		public function get_vars( ) {
			return $this->vars;
		}
	}
}
